package com.yyge.controller.admin.dashboard.strategy;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.yyge.common.constans.RedisConstans;
import com.yyge.controller.admin.dashboard.resp.ReportResp;
import com.yyge.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class CurrentMonthReport implements ContamerReportStrategy{
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    EmployeeService employeeService;

    @Override
    public ReportResp calcReport() {
       DateTime begin = DateUtil.beginOfMonth(new Date());
       DateTime end=DateUtil.endOfMonth(new Date());

       int beginDay= DateUtil.dayOfMonth(begin);
       int endDay=DateUtil.dayOfMonth(end);

        List<String> xAxis=new ArrayList<>();
        List<Object> series=new ArrayList<>();

        //根据redis的key
        int year=DateUtil.year(new Date());
        int month=DateUtil.month(new Date())+1;
        String ym= year+String.valueOf((month<10)?"0"+month: month);
        String redisKey= RedisConstans.getRedisKey(RedisConstans.CONTAMER_HASH_KEY,ym);

        //HMGET key field1 [field2] 获取所有给定字段的值
        Collection<String> multiFields=new ArrayList<>();
        for(int i=beginDay;i<=endDay;i++){
            multiFields.add(i+"");
        }

        List<Integer> list = this.redisTemplate.opsForHash().multiGet(redisKey, multiFields);
        //组装值
        for(int i=beginDay;i<=endDay;i++){
            //时间
            DateTime dateTime = DateUtil.offsetDay(begin, i-beginDay);
            //x坐标显示的值
            String str= DateUtil.format(dateTime,"MM/dd");
            xAxis.add(str);
            //在范围之内
            if(dateTime.isBefore(new Date())) {
                Integer value = list.get(i - beginDay);
                series.add(value==null?0:value);
            }else{
              series.add(0);
            }
        }

        ReportResp  reportResp=new ReportResp();
        reportResp.setXAxis(xAxis);
        reportResp.setSeries(series);
        return reportResp;
    }
}
